﻿namespace Mozi.Live.RTSP
{
    //npt-range | utc-range | smpte-range
    /// <summary>
    /// RTP时间区间描述
    /// <list type="table">
    /// <item>npt-range</item>
    /// <item>utc-range</item>
    /// <item>smpte-range</item>
    /// </list>
    /// </summary>
    public class RTPTimeRange
    {
        /// <summary>
        /// 时间单位
        /// </summary>
        public TimeRangeUnit Unit { get; set; }
        /// <summary>
        /// 开始时间
        /// </summary>
        public string StartTime { get; set; }
        /// <summary>
        /// 结束时间
        /// </summary>
        public string EndTime { get; set; }
        /// <summary>
        /// 转为字符串描述
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            return Unit.ToString().ToLower() + "=" + StartTime + "-" + EndTime;
        }
        /// <summary>
        /// 解析字符串描述的时间区间
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static RTPTimeRange Parse(string data)
        {
            string unit = data.Substring(0, data.IndexOf('='));
            string start = data.Substring(data.IndexOf('='), data.IndexOf('-') - data.IndexOf('='));
            string end = data.Substring(data.IndexOf('-'));
            RTPTimeRange range = new RTPTimeRange
            {
                Unit = (TimeRangeUnit)System.Enum.Parse(typeof(TimeRangeUnit), unit, true),
                StartTime = start,
                EndTime = end
            };
            return range;
        }
    }
    /// <summary>
    /// RTP支持的时间单位
    /// </summary>
    public enum TimeRangeUnit
    {
        NPT=1,
        Clock=2,
        SMPTE=3
    }
}
